package com.centling.smartSealForPhone.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.bean.ManagedUser;
import com.centling.smartSealForPhone.constants.SPConstants;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.constants.UserType;
import com.centling.smartSealForPhone.utils.ImageUtil;
import com.centling.smartSealForPhone.view.RoundImgView;

import java.util.List;

/**
 *
 * @author yuanye
 * @date 2016/5/30
 */
public class UserListAdapter
        extends RecyclerView.Adapter {
    private Context mContext;
    private List<ManagedUser> mList;
    private onItemClickListener listener;

    public UserListAdapter(Context context, List<ManagedUser> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(
                LayoutInflater.from(mContext).inflate(R.layout.item_user_manage, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;

        ImageUtil.loadAvatarImage(mList.get(position).getAvatar(),
                myViewHolder.avatar, R.drawable.iv_avatar_default);
        myViewHolder.realName.setText(mList.get(position).getRealName());
        myViewHolder.phone.setText(mList.get(position).getPhone());
        switch (mList.get(position).getUserType()) {
            case UserType.EQUIP_ADMIN:
                myViewHolder.userType.setText("设备管理员");
                break;
            case UserType.EQUIP_OPERATOR:
                myViewHolder.userType.setText("设备操作员");
                break;
            case UserType.EQUIP_COMMON_USER:
                myViewHolder.userType.setText("普通用章人");
                break;
            case UserType.EQUIP_OA_USER:
                myViewHolder.userType.setText("OA用户");
                break;
            case UserType.SERVER_USER:
                myViewHolder.userType.setText("审核员");
                break;
            default:
                myViewHolder.userType.setText("未知用户");
        }

        myViewHolder.department.setText(mList.get(position).getDepartment());

        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setOnItemClickListener(onItemClickListener l) {
        this.listener = l;
    }

    public interface onItemClickListener {
        void onClick(int position);
    }

    private class MyViewHolder
            extends RecyclerView.ViewHolder {
        public RoundImgView avatar;
        public TextView realName;
        public TextView phone;
        public TextView userType;
        public TextView department;

        MyViewHolder(View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.item_user_manage_avatar);
            realName = itemView.findViewById(R.id.item_user_manage_name);
            phone = itemView.findViewById(R.id.item_user_manage_phone);
            userType = itemView.findViewById(R.id.item_user_manage_type);
            department = itemView.findViewById(R.id.item_user_manage_department);
        }
    }
}
